<template>
  <div class="gk-layout-container">
    <div class="gk-layout-header">
      <!--工具栏-->
      <div class="toolbar" style="float:left">
        <el-form :inline="true" :model="filters" :size="global.gSize">
          <el-form-item>
            <el-select v-model="filters.classifyId" placeholder="请选择物料类别" style="width: 100%;" clearable>
              <el-option v-for="item in classTypes" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select v-model="filters.areaId" placeholder="请选择区域" auto-complete="off" style="width: 100%;" clearable>
              <el-option v-for="item in areaTypes" :key="item.id" :label="item.areaName" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        
          <el-form-item>
            <kt-button icon="fa fa-search" :label="$t('action.search')" perms="material:classifyarea:view"
              type="primary" @click="findPage(null)" />
          </el-form-item>
          <el-form-item>
            <kt-button icon="fa fa-plus" :label="$t('action.add')" perms="material:classifyarea:add" type="primary"
              @click="handleAdd" />
          </el-form-item>
        </el-form>
      </div>
      <div class="toolbar" style="float:right;padding-top:10px;padding-right:15px;">
        <el-form :inline="true" :size="global.gSize">
          <el-form-item>
            <el-button-group>
              <el-tooltip :content="$t('action.refresh')" placement="top">
                <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
              </el-tooltip>
             
             
            </el-button-group>
          </el-form-item>
        </el-form>
        <!--表格显示列界面-->
    
      </div>
    </div>
  


    
    <div class="gk-layout-main">
      <my-table :data="pageResult.content" :loading="loading"  @selection-change="selectionChange">
          <el-table-column :label="$t('action.desc')" width="50"    header-align="center" align="center">
            <template slot-scope="scope">   {{scope.$index+1}}   </template>
          </el-table-column> 
          <el-table-column type="selection" width="40" ></el-table-column>
          <el-table-column  header-align="center"  align="center"   prop="classifyId" label="类别名称"   key="classifyId" :formatter="selectionFormats" >  </el-table-column>
          <el-table-column  header-align="center"  align="center"   prop="areaId" label="区域名称"     key="areaId"  :formatter="selectionFormat">  </el-table-column>
          <el-table-column  header-align="center"  align="center"   prop="priority" label="优先级"     key="priority" :formatter="itemFilter">  </el-table-column>
          <el-table-column  header-align="center"  align="center"    :label="$t('action.operation')" >
              <template slot-scope="scope">
                <kt-delete-button    :label="$t('action.delete')"   perms="material:classifyarea:delete"  :size="global.gSize"   @click="handleDelete(scope.$index, scope.row)" />
                <kt-button icon="fa fa-edit"  :label="$t('action.edit')"  perms="material:classifyarea:edit"    @click="handleEdit(scope.$index, scope.row)" />
              </template>
          </el-table-column>
      </my-table>
  </div>
  <!--分页栏-->
  <div class="toolbar gk-layout-footer"  style="padding:10px;">
    <kt-delete-button   perms="material:classifyarea:delete" :size="global.gSize" type="danger"
    @click="handleBatchDelete()"  style="float:left;" />
    <el-pagination layout="total, prev, pager, next, jumper"  @current-change="findPage"
                   :current-page="pageRequest.pageNum"
                   :page-size="pageRequest.pageSize"
                   :total="pageResult.totalSize"
                   style="float:right;">
    </el-pagination>
  </div>



    <!--新增编辑界面-->
    <el-dialog :title="operation?'物料类别分区 —— 新增':'物料类别分区 —— 编辑'" width="40%" :visible.sync="dialogVisible"
      :close-on-click-modal="false">
      <el-form :model="dataForm" label-width="90px" :rules="dataFormRules" ref="dataForm" :size="global.gSize"
        label-position="right">
        <el-form-item label="ID" prop="id" v-if="false">
          <el-input v-model="dataForm.id" :disabled="true" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item :label="$t('item.classifyId')" prop="classifyId">
          <el-select v-model="dataForm.classifyId" placeholder="请选择物料类别" style="width: 100%;" clearable filterable>
            <el-option v-for="item in classTypes" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('field.Core.corewhloc.areaName')" prop="areaId">
          <el-select v-model="dataForm.areaId" placeholder="请选择区域" auto-complete="off" style="width: 100%;" clearable  filterable>
            <el-option v-for="item in areaTypes" :key="item.id" :label="item.areaName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="'优先级'" prop="priority">
          <el-select v-model="dataForm.priority" placeholder="请选择优先级" auto-complete="off" style="width: 100%;"
            clearable>
            <el-option v-for="item in dicts.classifyAreaPriority" :key="item.value" :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
       
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :size="global.gSize" @click.native="dialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button :size="global.gSize" type="primary" @click.native="submitForm" :loading="editLoading">{{$t('action.submit')}}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import PopupTreeInput from "@/components/PopupTreeInput"
  	import MyTable from "@/views/Core/MyTable"
  import KtButton from "@/views/Core/KtButton"
  import KtDeleteButton from "@/views/Core/KtDeleteButton"
 
  import { format } from "@/utils/datetime"
  export default {
    components: {
      PopupTreeInput,
  MyTable,
      KtButton,
      KtDeleteButton,
 
    },
    data() {
      return {
        size: 'small',
        filters: {
          name: ''
        },
        loading:false,
        dicts: this.$store.state.dict.dicts,
        columns: [],
        demo: [],
        filterColumns: [],
        pageRequest: { pageNum: 1, pageSize: 20 },
        pageResult: {},
        //dicts: [],
        operation: false, // true:新增, false:编辑
        dialogVisible: false,
        dialogVisibles: false,// 设置区位界面是否显示
        editLoading: false,
        selections: [],
        // 新增编辑界面数据
        dataForm: {
          id: 0,
          classifyId: '',
          areaId: '',
          priority: '2',
        },
        areaTypes: [],
        classTypes: []
      }
    },
    computed: {
      dataFormRules() {
        const dataFormRules = {
          classifyId: [{ required: true, message: "请选择物料类别", trigger: 'blur' }],
          areaId: [{ required: true, message: "请选择区域名称", trigger: 'blur' }]
        };
        return dataFormRules;
      }
    },
    methods: {
      // 获取分页数据
      findPage: function (data) {
          
        if (data !== null) {
          this.filters.pageNum = data 
        } else {
          this.filters.pageNum = 1
          this.pageRequest.pageNum = 1
        }
        this.filters.pageSize = this.pageRequest.pageSize
        this.$api.classifyarea.findPage(this.filters).then((res) => {
          this.pageResult = res.data
        }).then(data != null ? data.callback : '')
      },

      findAreaTypes: function () {
        this.$api.corewharea.findPage().then((res) => {
          this.areaTypes = res.data.content
        })
      },
      //加载物料类别分类
      findClassTypes: function () {
        this.$api.classify.findPage().then((res) => {
          this.classTypes = res.data.content
        })
      },

      //批量编辑
      submitForms: function () {
     
        //let para = this.handleBatchEdit(data).data
        let ppa = [];
        for (var i = 0; i < this.demo.selections.length; i++) {
          //alert(this.demo.params[i].id)
          ppa.push(this.demo.selections[i].id)
        }
        // let params = Object.assign({}, this.dataForm).areaId
        // console.log(params);
        this.$refs.dataForm.validate((valid) => {
          if (valid) {
            this.$confirm(this.getKey('action.isConfirm'), this.getKey('action.tips'), {}).then(() => {
              this.editLoading = true
              let params = Object.assign({}, this.dataForm).areaId
              this.$api.classifyarea.batchset(ppa, params).then((res) => {
                this.editLoading = false
                if (res.code == 200) {
                  this.$message({ message: this.getKey('action.operateSucess'), type: 'success' })
                  this.dialogVisibles = false
                  this.$refs['dataForm'].resetFields()
                } else {
                  this.$message({ message: this.getKey('action.operateFail') + res.msg, type: 'error' })
                }
                this.findPage(null)
              })
            })
          }
        })
      },
      selectionChange: function (selection) {

        this.selection = selection;
      },
      //显示批量设置库位界面
      handleBatchSet: function () {
        this.dialogVisibles = true
        this.operation = true
        this.demo = this.selection;
        console.log(this.demo);
      },



      findUserRoles: function () {
        this.$api.role.findAll().then((res) => {
          // 加载角色集合
          this.roles = res.data
        })
      },
      selectionChange: function (selection) {
          this.selections = selection;
      },
      handleBatchDelete: function () {
        let ids = this.selections.map(item => item.id).toString()
        let params = []
          let idArray = (ids + '').split(',')
          for (var i = 0; i < idArray.length; i++) {
            params.push({ 'id': idArray[i] })
          }
          this.$api.classifyarea.batchDelete(params).then((res) => {
         if(res.code=="500"){

          this.$message({ message: this.getKey('action.operateFail') + res.msg, type: 'error' })
         }
         this.findPage(null)
        });
      },
      // 批量删除
      handleDelete: function (index ,data) {
        let params = [{ 'id': data.id }]
        
        this.$api.classifyarea.batchDelete(params).then((res) => {
         if(res.code=="500"){

          this.$message({ message: this.getKey('action.operateFail') + res.msg, type: 'error' })
         }
         this.findPage(null)
        });
  
      },

      // 显示新增界面
      handleAdd: function () {
        this.dialogVisible = true
        this.operation = true
        this.dataForm = {
          id: 0,
          classifyId: '',
          areaId: '',
          priority: '',
        }
      },
      // 显示编辑界面
      handleEdit: function (params,row) {
        this.dialogVisible = true
        this.operation = false
        this.dataForm = Object.assign({},row)
      },
      // 编辑
      submitForm: function () {
        this.$refs.dataForm.validate((valid) => {
          if (valid) {
            this.$confirm(this.getKey('action.isConfirm'), this.getKey('action.tips'), {}).then(() => {
              this.editLoading = true
              let params = Object.assign({}, this.dataForm)
              this.$api.classifyarea.save(params).then((res) => {
                this.editLoading = false
                if (res.code == 200) {
                  this.$message({ message: this.getKey('action.operateSucess'), type: 'success' })
                  this.dialogVisible = false
                  this.$refs['dataForm'].resetFields()
                } else {
                  this.$message({ message: this.getKey('action.operateFail') + res.msg, type: 'error' })
                }
                this.findPage(null)
              })
            })
          }
        })
      },
      
    
      // 处理表格列过滤显示
      handleFilterColumns: function (data) {
        this.filterColumns = data.filterColumns
        this.$refs.tableColumnFilterDialog.setDialogVisible(false)
      },
      getKey: function (arg) {
        return this.$t(arg)
      },
 
      selectionFormat: function (row, column, cellValue, index) {
        let key = ""
        let propt = column.property;
        let val = row[column.property];
        let dict = this.areaTypes;
        for (let i = 0; i < dict.length; i++) {
          if (dict[i].id == val) {
            return dict[i].areaName;
          }
        }
        return row[column.property]
      },
      itemFilter: function (row, column, val, index) {

        //let val=row["isSorting"];
        let dict = this.$store.state.dict.dicts["classifyAreaPriority"];
        if (dict == undefined) {
          return val
        }
        for (let i = 0; i < dict.length; i++) {
          if (dict[i].value == val) {
            return dict[i].label;
          }
        }
        return val
      },
      selectionFormats: function (row, column, cellValue, index) {
        let key = ""
        let propt = column.property;
        let val = row[column.property];
        let dict = this.classTypes;
        for (let i = 0; i < dict.length; i++) {
          if (dict[i].id == val) {
            return dict[i].name;
          }
        }
        return row[column.property]
      },
    },
    created() {

 
      this.findAreaTypes();
      this.findClassTypes();
     
this. dicts=this.$store.state.dict.dicts;
this. findPage(null);

    }
  }
</script>

<style scoped>
</style>